//
//  RBCompeExpZsListCell.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/13.
//

#import "RBCompeExpZsListCell.h"
#import "RBCompExpZsCollectionCell.h"
#import "RBCompeExpZsModel.h"

@interface RBCompeExpZsListCell ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>
@property(strong,nonatomic)UIView *bgView;
@property(strong,nonatomic)UILabel *titleLabel;
@property(strong,nonatomic)UICollectionView *collectionView;
@property(strong,nonatomic)NSMutableArray *dataArray;

@end

@implementation RBCompeExpZsListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.dataArray = [NSMutableArray array];
        
        [self rb_initViewUI];
        [self setNeedsLayout];
    }
    return self;
}
-(void)rb_initViewUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.collectionView];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(RBScaleWidth(15));
        make.left.mas_equalTo(RBScaleWidth(10));
        make.height.mas_equalTo(RBScaleWidth(14));
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(RBScaleWidth(10));
        make.left.mas_equalTo(RBScaleWidth(10));
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.bottom.mas_equalTo(-RBScaleWidth(10));
    }];
}
-(void)setModel:(RBCompeExpZsModel *)model{
    _model=model;
    self.dataArray = [NSMutableArray array];
    
    if ([model.forecastType isEqualToString:@"dxf"]){ // 大小分
        self.titleLabel.text = @"大小分";
        NSArray *valueArray = @[model.dxf.sf2,model.dxf.sf1,model.dxf.sf3];
        NSArray *keyArray = @[@"大分",@"即时数据",@"小分"];
        for (int i=0; i<valueArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            modelselect.itemName = keyArray[i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
        }
    }
    else if ([model.forecastType isEqualToString:@"rqsf"]){ // 最后进行数据分离
        self.titleLabel.text = @"让球胜负";
        NSArray *valueArray = @[model.rqsf.sf0,model.rqsf.sf1,model.rqsf.sf2];
        NSArray *keyArray = @[@"主胜",@"让球",@"主负"];
        for (int i=0; i<valueArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            if (i==1) {
                if ([model.rqsf.sf1 containsString:@"-"]) {
                    valueStr = [NSString stringWithFormat:@"客%@",valueStr];
                }else{
                    valueStr = [NSString stringWithFormat:@"主-%@",valueStr];
                }
            }
            
            // 判读为0的时候
            if([valueStr isEqualToString:@"主-0"]){
                valueStr = [NSString stringWithFormat:@"主%@",model.rqsf.sf1];
            }
            
            modelselect.itemName = keyArray[i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
            
        }
    }
    else if ([model.forecastType isEqualToString:@"rfsf"]){ // 最后进行数据分离
        self.titleLabel.text = @"让分胜负";
        NSArray *valueArray = @[model.rfsf.sf0,model.rfsf.sf2,model.rfsf.sf1];
        NSArray *keyArray = @[@"让分",@"主负",@"主胜"];
        for (int i=0; i<valueArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            if (i==0) {
                valueStr = [NSString stringWithFormat:@"主%@",valueStr];
            }
            modelselect.itemName = keyArray[i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
        }
    }
    else if ([model.forecastType isEqualToString:@"rqspf"]){ // 最后进行数据分离
        
        self.titleLabel.text = @"让球";
        NSArray *valueArray = @[model.rqspf.sf0,model.rqspf.sf1,model.rqspf.sf2,model.rqspf.sf3];
        NSArray *keyArray = @[@"让球",@"让胜",@"让平",@"让负"];
        for (int i=0; i<keyArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            if (i==0) {
                valueStr = [NSString stringWithFormat:@"主%@",valueStr];
            }
            modelselect.itemName = keyArray[i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
        }
    }
    else if ([model.forecastType isEqualToString:@"dxq"]){
        
        self.titleLabel.text = @"大小球";
        
        NSArray *valueArray = @[model.dxq.sf1,model.dxq.sf2,model.dxq.sf3];
        NSArray *keyArray = @[@"大球",@"即时数据",@"小球"];
        for (int i=0; i<valueArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            modelselect.itemName = keyArray[i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
        }
        
    }
    
    else if ([model.forecastType isEqualToString:@"spf"]){
        
        self.titleLabel.text = @"胜平负";
        NSArray *valueArray = @[model.spf.sf3,model.spf.sf1,model.spf.sf0];
        NSArray *keyArray = @[@"胜",@"平",@"负"];
        for (int i=0; i<valueArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            modelselect.itemName = keyArray[i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
        }
    }
    else if ([model.forecastType isEqualToString:@"jq"]) {
        
        self.titleLabel.text = @"总进球";
        NSArray *valueArray = @[model.jq.j0,model.jq.j1,model.jq.j2,model.jq.j3,model.jq.j4,model.jq.j5,model.jq.j6,model.jq.j7];
        for (int i=0; i<valueArray.count; i++) {
            RBSelectMethodsModel *modelselect = [[RBSelectMethodsModel alloc]init];
            NSString *valueStr = valueArray[i];
            modelselect.itemName = [NSString stringWithFormat:@"%d",i];
            modelselect.odds = valueStr;
            modelselect.result = model.result;
            modelselect.matchResult = model.matchResult;
            modelselect.articleFinish = model.articleFinish; // 以这个是否结束作为主键判断
            [self.dataArray addObject:modelselect];
        }
    }
    
    [self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RBSelectMethodsModel *selectModel = self.dataArray[indexPath.item];
    RBCompExpZsCollectionCell *compExpZsCollectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RBCompExpZsCollectionCell" forIndexPath:indexPath];
    if ([_model.forecastType isEqualToString:@"jq"]) {
        compExpZsCollectionCell.isDisPlay = YES;
    }
    compExpZsCollectionCell.indexPath = indexPath;
    compExpZsCollectionCell.forecastResultArray = self.forecastResultArray;
    compExpZsCollectionCell.expModel = _model;
    compExpZsCollectionCell.selectModel = selectModel;
    return compExpZsCollectionCell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((RBScreenWidth-RBScaleWidth(20))/self.widthCount, RBScaleWidth(60));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (UIView *)bgView{
    if(_bgView==nil){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
-(UILabel *)titleLabel{
    if(_titleLabel==nil){
        _titleLabel = [UILabel rb_labelWithTitle:@"总进球" color:RBColor(51, 51, 51) font:RBPingFangMediumFont(14) alignment:NSTextAlignmentLeft];
    }
    return _titleLabel;
}
-(UICollectionView *)collectionView{
    if(_collectionView==nil){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[RBCompExpZsCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([RBCompExpZsCollectionCell class])];
    }
    return _collectionView;
}

@end
